﻿{{ extend '../_layouts/user.html'}}
{{ block 'left' }}
<ul id="leftbar">
    <li class="now"><a href="/userset">基本信息</a></li>
    <li><a href="/safeset">安全中心</a></li>
    <li>其它</li>
</ul>
{{ /block }}


{{ block 'right' }}
<form id="form-submit">
    <input type="hidden" name="id" value="{{ user._id }}">
    <div class="baseinfo">
        <div class="baseinfo-left">
            <img src="{{ user.avatar }}" alt="">
            <input type="file" name="avatar" id="avatar" class="my-form">
        </div>
        <div class="baseinfo-right form-group">
            <label>
                邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱
                <input type="text" name="email" id="email" class="my-form" value="{{ user.email }}" disabled>
            </label><br>
            <label>
                昵&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称
                <input type="text" name="nickname" id="nickname" class="my-form" value="{{ user.nickname }}">
            </label>
            
            <div class="txtarea">
                <span>个人简介</span>   
                <textarea name="bio" id="bio" rows="5" class="form-control" id="txtarea">
                    {{ user.bio }}
                </textarea>
            </div>
            <div class="gender">
                    <span>性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别</span>
                    <input type="radio" id="radio1" name="gender" value="-1">
                    <label for="radio1">保密</label>
                    <input type="radio" id="radio2" name="gender" value="0" >
                    <label for="radio2">男</label>
                    <input type="radio" id="radio3" name="gender" value="1" >
                    <label for="radio3">女</label>
                    
            </div>
            <div class="birthday">
                <label>
                    生&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;日
                    <select class="my-form" name="year" id="year">
                    </select>
                    <span>年</span>
                    <select class="my-form" name="mouth" id="mouth">
                    </select>
                    <span>月</span>
                    <select class="my-form" name="day" id="day">
                    </select>
                    <span>日</span>
                </label>
            </div>
        </div>
    </div>
    <button type="submit" class="btn btn-default submit">修改</button>
</form>
{{ /block }}

{{ block 'script' }}
<script>
    let year = $('#year')
    let mouth = $('#mouth')
    let day = $('#day')
    getDate(year,1990,2020)
    getDate(mouth,1,12)
    getDate(day,1,31)
    function getDate(obj,start,end){
        let temStr = ''
        for(let i = start; i< end+1;i++){
        temStr += '<option value="'+i+'">'+i+'</option>'
        }
        obj[0].innerHTML = temStr
    }
    year.val('{{ user.year}}')
    mouth.val('{{ user.mouth}}')
    day.val('{{ user.day}}')
    $("input[name='gender'][value={{ user.gender }}]").attr("checked",true);
    // let gender = '{{ user.gender }}'
    //     if (gender == -1) {
    //         document.getElementById('radio1').checked = true
    //     } else if(gender == 0) {
    //         document.getElementById('radio2').checked = true
    //     } else {
    //         document.getElementById('radio3').checked = true
    //     }
    $('#form-submit').on('submit', function (e) {
      e.preventDefault()
      var formData = $(this).serialize()
      $.ajax({
        url: '/userset',
        type: 'post',
        data: formData,
        dataType: 'json',
        success: function (data) {
          if(data.err_code==0){
              window.alert('修改成功！')
              window.location.href = '/userset'
          }else{
              window.alert('修改失败！')
          }
        }
      })
    })
</script>
{{ /block }}